package om.test.dao;

import com.test.dao.UserMapper;
import com.test.pojo.User;
import com.test.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * 注解方式的增删改查
 */

public class Daotest1 {
    @Test
    public void test5(){
        // 修改
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int upNum = mapper.upUser("小南", "456789", 9);
        System.out.println(upNum);
        sqlSession.close();
    }

    @Test
    public void test4(){
        // 删除
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int delNum = mapper.delUser(9);
        System.out.println(delNum);
        sqlSession.close();
    }

    @Test
    public void test3(){
        // 添加
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int insertNum = mapper.addUser(new User("小刘", "123456"));
        System.out.println(insertNum);
        sqlSession.close();
    }

    @Test
    public void test2(){
        // 根据id查询
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(1);
        System.out.println(user);
        sqlSession.close();
    }
    @Test
    public void test1(){
        //查询所有
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUsers();
        System.out.println(userList);
        sqlSession.close();
    }
}
